package com.kyomonn.talkischeap.QuestionBank;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kyomonn.talkischeap.R;

public class QuestionAdapter extends BaseAdapter {
    private final Context context;
    private final int resource;
    private final QuestionItem[] questionItems;

    public QuestionAdapter(Context context, int resource, QuestionItem[] questionItems) {
        this.context = context;
        this.resource = resource;
        this.questionItems = questionItems;
    }

    @Override
    public int getCount() {
        return questionItems.length;
    }

    @Override
    public Object getItem(int i) {
        return questionItems[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;

        if (convertView == null) {
            // 加载 item 布局
            convertView = View.inflate(context, resource, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // 获取当前题目数据
        QuestionItem item = questionItems[i];
        if (item != null) {
            holder.bind(item, i + 1, context);
        } else {
            // 设置默认值或占位符
            holder.bind(new QuestionItem("无效题目", new String[]{"", "", "", ""}, 0), 0, context);
        }

        return convertView;
    }

    static class ViewHolder {
        TextView questionIndex;
        TextView questionText;
        TextView optionTextA;
        TextView optionTextB;
        TextView optionTextC;
        TextView optionTextD;

        private boolean isSelectedA, isSelectedB, isSelectedC, isSelectedD;

        public ViewHolder(View view) {
            questionIndex = view.findViewById(R.id.question_index);
            questionText = view.findViewById(R.id.question_text);
            optionTextA = view.findViewById(R.id.option_text_A);
            optionTextB = view.findViewById(R.id.option_text_B);
            optionTextC = view.findViewById(R.id.option_text_C);
            optionTextD = view.findViewById(R.id.option_text_D);
        }

        public void bind(QuestionItem questionItem, int index, Context context) {
            final int questionPosition = index - 1; // 因为 index 是从 1 开始的

            questionIndex.setText(String.valueOf(index));
            questionText.setText(questionItem.getQuestionText());

            optionTextA.setText(questionItem.getOptions()[0]);
            optionTextA.setOnClickListener(view -> {
                setSelected(optionTextA, optionTextB, optionTextC, optionTextD, context, questionPosition, 0);
            });

            optionTextB.setText(questionItem.getOptions()[1]);
            optionTextB.setOnClickListener(view -> {
                setSelected(optionTextA, optionTextB, optionTextC, optionTextD, context, questionPosition, 1);
            });

            optionTextC.setText(questionItem.getOptions()[2]);
            optionTextC.setOnClickListener(view -> {
                setSelected(optionTextA, optionTextB, optionTextC, optionTextD, context, questionPosition, 2);
            });

            optionTextD.setText(questionItem.getOptions()[3]);
            optionTextD.setOnClickListener(view -> {
                setSelected(optionTextA, optionTextB, optionTextC, optionTextD, context, questionPosition, 3);
            });
        }

        private void setSelected(TextView a, TextView b, TextView c, TextView d, Context context, int position, int selected) {
            a.setBackgroundColor(context.getResources().getColor(selected == 0 ? R.color.colorPrimary : R.color.white));
            b.setBackgroundColor(context.getResources().getColor(selected == 1 ? R.color.colorPrimary : R.color.white));
            c.setBackgroundColor(context.getResources().getColor(selected == 2 ? R.color.colorPrimary : R.color.white));
            d.setBackgroundColor(context.getResources().getColor(selected == 3 ? R.color.colorPrimary : R.color.white));

            // 通知 Activity 用户选择了哪个选项
            ((QuestionPanelActivity) context).setUserAnswer(position, selected);
        }
    }
}
